package com.skyline.search.search.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.paoding.analysis.analyzer.PaodingAnalyzer;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import com.skyline.search.model.Page;
import com.skyline.search.model.ResultSet;
import com.skyline.search.search.Searcher;
import com.skyline.search.util.SearchUtil;

/**
 * Class <code>SearcherImpl</code> SearcherImpl是对Searcher的实现
 * 
 * @author Jairus Chan
 * @version 0.1, 11/30/10
 * @since JDK1.6
 */

public class SearcherImpl implements Searcher {

	private Directory directory;
	private Analyzer defaultAnalyzer = null;
	private Analyzer standardAnalyzer = null;
	private IndexSearcher indexSearcher = null;

	@SuppressWarnings("deprecation")
	private void intialAnalyzer() {
		if (defaultAnalyzer == null) {
			defaultAnalyzer = new PaodingAnalyzer();
		}
		if (standardAnalyzer == null) {
			standardAnalyzer = new StandardAnalyzer();
		}
	}

	public void inital(String indexPath) {
		try {
			if (SearchUtil.checkSubFileExists(indexPath) == false)
				return;
			directory = FSDirectory.open(new File(indexPath));
			//indexSearcher = new IndexSearcher(directory);
			intialAnalyzer();
			System.out.println("初始化Searcher--" + indexPath + "完成!");
		} catch (IOException e) {

			System.out.println("初始化Searcher--" + indexPath + "失败:Index还没有创建!");
			e.printStackTrace();
		}
	}

	public ResultSet searchWithDefaultAnalyzer(String queryString, Page page,
			String[] fields) {
		return search(queryString, page, fields, defaultAnalyzer);
	}

	@Override
	public ResultSet searchWithStandardAnalyzer(String queryString, Page page,
			String[] fields) {
		return search(queryString, page, fields, standardAnalyzer);

	}

	@SuppressWarnings("deprecation")
	public ResultSet search(String queryString, Page page, String[] fields,
			Analyzer analyzer) {
		try {
			indexSearcher=new IndexSearcher(directory);
			
			int firstResult = (page.getCurpage() - 1) * page.getSize();
			int maxResult = page.getSize();
			List<Document> documents = new ArrayList<Document>();
			
			QueryParser queryParser = new MultiFieldQueryParser(fields,
					analyzer);
			Query query = queryParser.parse(queryString);
			
			Filter filter = null;
			Sort sort=new Sort();
			sort.setSort("id", true);
			TopDocs topDocs = indexSearcher.search(query, filter, 10000,sort);
			
			
			int total = topDocs.totalHits;
			page.setTotal(total);
			// System.out.println("total:" + total);
			int end = Math.min(firstResult + maxResult, total);
			for (int i = firstResult; i < end; i++) {
				ScoreDoc scoreDoc = topDocs.scoreDocs[i];
				int docSn = scoreDoc.doc;
				// System.out.println(docSn);
				Document doc = indexSearcher.doc(docSn);
				documents.add(doc);
			}
			indexSearcher.close();
			ResultSet result = new ResultSet();
			result.setPage(page);
			result.setDocuments(documents);
			return result;
		} catch (Exception e) {
			ResultSet result = new ResultSet();
			result.setPage(page);
			List<Document> documents = new ArrayList<Document>();
			result.setDocuments(documents);
			e.printStackTrace();
			return result;
		}
	}
}
